package com.thinksns.sociax.t4.android.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.adapter.AdapterMyFriends;
import com.thinksns.sociax.t4.android.Thinksns;

import com.thinksns.sociax.t4.component.ListSociax;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient;
import com.thinksns.sociax.t4.android.data.StaticInApp;
import com.thinksns.sociax.t4.android.user.ActivityUserInfo_2;
import com.thinksns.sociax.t4.android.widget.ContactItemInterface;
import com.thinksns.sociax.t4.android.widget.ContactListViewImpl;
import com.thinksns.sociax.t4.model.ModelSearchUser;
import com.thinksns.sociax.thinksnsbase.activity.widget.LoadingView;
import com.thinksns.sociax.thinksnsbase.exception.ApiException;
import com.thinksns.sociax.thinksnsbase.utils.ActivityStack;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * 类说明：   互相关注的好友
 *
 * @author Zoey
 * @version 1.0
 * @date 2015年11月10日
 */
public class FragmentMyFriends extends FragmentSociax implements TextWatcher {

    private ContactListViewImpl listviewFriend;
    private EditText searchBox;
    private String searchString;

    private Object searchLock = new Object();
    boolean inSearchMode = false;

    List<ContactItemInterface> contactList;
    List<ContactItemInterface> filterList;
    private SearchListTask curSearchTask = null;
    private AdapterMyFriends adapter = null;
    private LinearLayout ll_default;

    private LoadingView loadingView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (adapter == null) {
            getFriendsList();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my_friends;
    }

    @Override
    public void initView() {
        filterList = new ArrayList<ContactItemInterface>();
        ll_default = (LinearLayout) view.findViewById(R.id.ll_default);
        loadingView = (LoadingView) view.findViewById(LoadingView.ID);
        listviewFriend = (ContactListViewImpl) view.findViewById(R.id.lv_my_friends);
        listviewFriend.setFastScrollEnabled(true);
        searchBox = (EditText) view.findViewById(R.id.input_search_query);
    }

    /**
     * 获取好友列表
     */
    public void getFriendsList() {
        loadingView.show(listviewFriend);
        Thinksns app = (Thinksns) getActivity().getApplicationContext();
        try {
            app.getUsers().getUserFriendsList(Thinksns.getMy().getUid(), 0, mListener);
        } catch (ApiException e) {
            e.printStackTrace();
            loadingView.hide(listviewFriend);
        }
    }

    private ApiHttpClient.HttpResponseListener mListener = new ApiHttpClient.HttpResponseListener() {
        @Override
        public void onSuccess(Object result) {
            Message msg = new Message();
            msg.what = StaticInApp.GET_FRIENDS_EACHOTHER;
            msg.obj = result;
            handler.sendMessage(msg);
        }

        @Override
        public void onError(Object result) {
            Toast.makeText(getActivity(), result.toString(), Toast.LENGTH_SHORT).show();
        }
    };

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case StaticInApp.GET_FRIENDS_EACHOTHER:
                    try {
                        contactList = (List<ContactItemInterface>) msg.obj;
                        if (contactList == null) {
                            return;
                        }
                        if (contactList.size() == 0) {
                            ll_default.setVisibility(View.VISIBLE);
                        } else {
                            ll_default.setVisibility(View.GONE);
                            adapter = new AdapterMyFriends(getActivity(), R.layout.list_item_my_friends,
                                    contactList, StaticInApp.CONTACTS_LIST_FRIENDS, false);
                            listviewFriend.setAdapter(adapter);
                        }
                        loadingView.hide(listviewFriend);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    public void initIntentData() {

    }

    @Override
    public void initListener() {
        searchBox.addTextChangedListener(this);
        listviewFriend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle data = new Bundle();
                ModelSearchUser searchUser = (ModelSearchUser) view.getTag(R.id.tag_search_user);
                if (searchUser == null) {
                    Log.v("ListUser--onClick", "wztest tag null");
                    return;
                }
                Log.v("ListUser--onClick", searchUser.getUname() + searchUser.getUid());
                data.putInt("uid", searchUser.getUid());
                data.putString("name", searchUser.getUname());
                ActivityStack.startActivity(getActivity(), ActivityUserInfo_2.class, data);
            }
        });
        ll_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFriendsList();
            }
        });
    }

    @Override
    public void initData() {
        getFriendsList();
    }

    private class SearchListTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            filterList.clear();
            String keyword = params[0];
            inSearchMode = (keyword.length() > 0);
            if (inSearchMode) {
                for (ContactItemInterface item : contactList) {
                    ModelSearchUser contact = (ModelSearchUser) item;
                    boolean isPinyin = contact.getUname().toUpperCase().indexOf(keyword) > -1;
                    boolean isPinyin1 = contact.getRemark().toUpperCase().indexOf(keyword) > -1;
                    boolean isChinese = contact.getUname().indexOf(keyword) > -1;
                    boolean isChinese1 = contact.getRemark().indexOf(keyword) > -1;

                    if (isPinyin || isChinese || isPinyin1 || isChinese1) {
                        filterList.add(item);
                    }
                }
            }
            return null;
        }

        protected void onPostExecute(String result) {

            synchronized (searchLock) {
                if (inSearchMode) {
                    AdapterMyFriends adapter = new AdapterMyFriends(getActivity(), R.layout.list_item_my_friends, filterList, StaticInApp.CONTACTS_LIST_FRIENDS, false);
                    adapter.setInSearchMode(true);
                    listviewFriend.setInSearchMode(true);
                    listviewFriend.setAdapter(adapter);
                } else {
                    if (contactList != null) {
                        AdapterMyFriends adapter = new AdapterMyFriends(getActivity(), R.layout.list_item_my_friends, contactList,
                                StaticInApp.CONTACTS_LIST_FRIENDS, false);
                        adapter.setInSearchMode(false);
                        listviewFriend.setInSearchMode(false);
                        listviewFriend.setAdapter(adapter);
                    }
                }
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        searchString = searchBox.getText().toString().trim().toUpperCase();

        if (curSearchTask != null
                && curSearchTask.getStatus() != AsyncTask.Status.FINISHED) {
            try {
                curSearchTask.cancel(true);
            } catch (Exception e) {
            }
        }
        curSearchTask = new SearchListTask();
        curSearchTask.execute(searchString);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Subscribe
    public void refresh(ModelUser user) {
        if (user.getUid() != Thinksns.getMy().getUid()) {
            getFriendsList();
        }
    }
}
